//
//  HMNewFeatureViewController.swift
//  Weibo
//
//  Created by 李学林 on 15/12/9.
//  Copyright © 2015年 tete. All rights reserved.
//

import UIKit

class HMNewFeatureViewController: UIViewController ,UIScrollViewDelegate{

    override func viewDidLoad() {
        super.viewDidLoad()
        setupUI()

    }

    private func setupUI(){
        scrollView.frame = view.bounds
        
        view.addSubview(scrollView)
        pageControl.frame = CGRect(x: scrollView.frame.width * 0.5 , y: scrollView.frame.height - 100, width: 0, height: 0)
        view.addSubview(pageControl)
    
        let imageCount = 4
        for i in 0..<imageCount {
        
            let imageView = UIImageView(image: UIImage(named: "new_feature_\(i+1)"))
            
            imageView.frame = CGRect(x: CGFloat(i) * scrollView.frame.size.width, y: 0 , width: scrollView.frame.size.width, height: scrollView.frame.size.height)
            scrollView.addSubview(imageView)
            if i == imageCount - 1 {
            
                setupLastPage(imageView)
            }

        }
        scrollView.contentSize = CGSize(width:CGFloat(imageCount)*scrollView.frame.width, height: 0)
        
        pageControl.numberOfPages = imageCount
    }
    
    private func setupLastPage(imageView:UIView) {
        imageView.userInteractionEnabled = true
        loginButton.center = CGPoint(x: imageView.width * 0.5, y: imageView.frame.height - 150)
        shareButton.center = CGPoint(x: loginButton.center.x, y: CGRectGetMinY(loginButton.frame) - 30)
        imageView.addSubview(loginButton)
        imageView.addSubview(shareButton)
    }
    
    

    func scrollViewDidScroll(scrollView: UIScrollView) {
        
        let page = scrollView.contentOffset.x / scrollView.width + 0.5
        pageControl.currentPage = Int(page);
        
    }
    
    
    // 懒加载控件
    private lazy var scrollView: UIScrollView = {
        
        let scrollView = UIScrollView()
        scrollView.delegate = self;
        scrollView.pagingEnabled = true
        scrollView.showsHorizontalScrollIndicator = false
        scrollView.bounces = false
        
        return scrollView
    }()
    
    // 懒加载pageControl
    private lazy var pageControl :UIPageControl = {
    
        let pageControl = UIPageControl()
        pageControl.currentPageIndicatorTintColor = UIColor.redColor()
        pageControl.pageIndicatorTintColor = UIColor.yellowColor()
        return pageControl
    }()
    
    // 懒加载'立即登录'
    private lazy var loginButton: UIButton = {
    
        let loginButton = UIButton()
        loginButton.addTarget(self, action: "enterWBClick", forControlEvents: .TouchUpInside)
        loginButton.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState: UIControlState.Selected)
        loginButton.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: UIControlState.Normal)
        loginButton.setTitle("进入微博", forState: UIControlState.Normal)
        loginButton.sizeToFit()
        return loginButton
    
    }()
    @objc private func enterWBClick(){
        // 切换界面
        printLog("进入微博")
        NSNotificationCenter.defaultCenter().postNotificationName(HMSwitchRootVCNotifation, object: nil)

    }
    
    // 懒加载'分享按钮'
    private lazy var  shareButton: UIButton = {
        let shareButton = UIButton()
        shareButton.addTarget(self, action: "shareClick:", forControlEvents: .TouchUpInside)
        shareButton.setImage(UIImage(named: "new_feature_share_true"), forState: UIControlState.Selected)
        shareButton.setImage(UIImage(named: "new_feature_share_false"), forState: UIControlState.Normal)
        shareButton.setTitle("分享到微博", forState: UIControlState.Normal)
        shareButton.setTitleColor(UIColor.darkGrayColor(), forState: UIControlState.Normal)
        shareButton.titleEdgeInsets = UIEdgeInsets(top: 0, left: 5, bottom: 0, right: -5)
        shareButton.sizeToFit()
        return shareButton
    }()
    
    @objc private func shareClick(button:UIButton){
        button.selected = !button.selected;
    
    }



    
}
